﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using Gift.Model;
using Gift.DAL;

namespace WebSite.cn
{
    public partial class account : BasePage
    {
        // Fields
        public string address = "";
        public string carid = "0";
        public string carinfo = "";
        public string cname = "";
        public string email = "";
        public string istrue = "0";
        public string phone = "";
        public string post = "";
        public string score1 = "0";
        public string score2 = "0";
        public string score3 = "0";
       // public string shipmsg = "";
        public string tel = "";
        public string totalprice = "0";
        public string totalscore = "0";
        public string accountmoney = "0";
        public string sendDate = DateTime.Now.AddDays(3).ToString("yyyy-MM-dd");

        public string isHaveCard = "0";
        public string tempOrderCode = "";

        public string UserCardTotalPrice = "";

        readonly MemberAccount theMemberAccount = new MemberAccount();
        readonly Member theMember = new Member();
        readonly OrderAccount theOrderAccount = new OrderAccount();

        public string TempType
        {
            get{

                if (base.UserCardCodeList != null)
                {
                    return "1";
                }
                else if (base.UserMemberId != null)
                {
                    return "2";
                }
                else
                {
                    return Request.QueryString["type"];
                }
            }
        }

        public string BackHref
        {
            get {
                if (Session["backHref"] != null)
                {
                    return Session["backHref"].ToString() + "&isView=1";
                }
                else
                {
                    return "#";
                }
            }
        }

        // Methods
        private void Bind()
        {
          //  int a = (int)DateTime.Parse("").DayOfWeek;
            DataTable table;
            if (base.UserMemberId != null)
            {
                table = SQLAccess.GreatDs("select membername,tel1,phone,post,address,email,score1,score2,score3 from DB_Member where memberid=" + UserMemberId + "").Tables[0];
                if (table.Rows.Count > 0)
                {
                    this.cname = table.Rows[0]["membername"].ToString();
                    this.tel = table.Rows[0]["tel1"].ToString();
                    this.phone = table.Rows[0]["phone"].ToString();
                    this.post = table.Rows[0]["post"].ToString();
                    this.address = table.Rows[0]["address"].ToString();
                    this.email = table.Rows[0]["email"].ToString();
                    this.score1 = table.Rows[0]["score1"].ToString();
                    this.score2 = table.Rows[0]["score2"].ToString();
                    this.score3 = table.Rows[0]["score3"].ToString();
                  //  this.sendDate = table.Rows[0]["sendDate"].ToString();
                }
            }
            if (base.Request.QueryString["action"] != null)
            {
           
                string str3 = re.replace(base.Request.QueryString["carid"]);
                if (re.replace(base.Request.QueryString["action"]) == "edit")
                {
                    string str2 = re.replace(base.Request.Form["car_" + str3 + ""]);
                    try
                    {
                       int iNum =  int.Parse(str2);
                       if (iNum <= 0)
                       {
                           base.Response.Write("<script language='javascript'>alert('数量只能为大于0的整数!');window.location.href='account.aspx';</script>");
                           base.Response.End();
                           return;

                       }
                    }
                    catch {
                      
                        base.Response.Write("<script language='javascript'>alert('数量只能为大于0的整数!');window.location.href='account.aspx';</script>");
                        base.Response.End();
                        return;
                    }
                    table = SQLAccess.GreatDs("select top 1 carid,pronum,price,score from DB_Car where carid=" + str3 + " and temp=" + GetTempId() ).Tables[0];
                    if (table.Rows.Count > 0)
                    {
                        SQLAccess.DoSql("update DB_Car set pronum=" + str2 + ",sumprice=" + Convert.ToString((float)(Convert.ToSingle(str2) * Convert.ToSingle(table.Rows[0]["price"]))) + ",sumscore=" + Convert.ToString((float)(Convert.ToSingle(str2) * Convert.ToSingle(table.Rows[0]["score"]))) + " where carid=" + table.Rows[0]["carid"].ToString() + " and Temp=" +  GetTempId());
                        base.Response.Write("<script language='javascript'>alert('修改成功!');window.location.href='account.aspx';</script>");
                        base.Response.End();
                    }
                }
                if (re.replace(base.Request.QueryString["action"]) == "del")
                {
                    SQLAccess.DoSql("delete from DB_Car where carid=" + str3 + " and temp=" + GetTempId());
                    base.Response.Write("<script language='javascript'>alert('删除成功!');window.location.href='account.aspx';</script>");
                    base.Response.End();
                }
            }
            if (((re.ReadCookie("tempno") == null) || (re.ReadCookie("tempno") == "")) && UserCardCodeList == null)
            {
                this.carinfo = "<tr><td align='center' colspan='6' height=45>您的购物车中无商品</td></tr>";
            }
            else
            {
                string sTempId = GetTempId();
                table = SQLAccess.GreatDs("select DB_Car.carid,DB_Car.pronum,DB_Car.price,DB_Car.sumprice,DB_Car.sumscore,DB_Car.contents,"
                                         + " isnull(DB_Shoppro.proisnew2,0) as proisnew2,isnull(DB_Shoppro.proisnew3,0) as proisnew3,isnull(DB_Shoppro.proisnew4,0) as proisnew4,DB_Shoppro.typeid,DB_Shoppro.proname,DB_Shoppro.proprice1,DB_Shoppro.propic2,DB_Shoppro.proid  from DB_Car "
                                          + " inner join DB_Shoppro on DB_Car.productid=DB_Shoppro.proid where isnull(DB_Shoppro.proku1,0) >0 and DB_Car.flag=0 and DB_Car.temp=" + sTempId + "").Tables[0];
                if (table.Rows.Count > 0)
                {
                    this.istrue = "1";
                    foreach (DataRow row in table.Rows)
                    {
                        if (isHaveCard == "0")
                        {
                            if (bool.Parse(row["proisnew3"].ToString()) == true)
                            {
                                isHaveCard = "1";
                            }
                        }

                        this.carid = this.carid + "," + row["carid"].ToString();
                        this.totalprice = string.Format("{0:f}", Convert.ToSingle(row["sumprice"]) + Convert.ToSingle(this.totalprice));
                        this.totalscore = Convert.ToString((uint)(Convert.ToUInt32(row["sumscore"]) + Convert.ToUInt32(this.totalscore)));
                        this.carinfo = this.carinfo + "<tr>";
                        string carinfo = this.carinfo;
                        this.carinfo = carinfo + "<td align='center'><a href='products_details.aspx?pid=" + row["proid"].ToString() + "'><img src='../uploadfile/product/" + row["propic2"].ToString() + "' align='left' border='0'  width='106' /></a></td>";
                        string str6 = this.carinfo;
                        this.carinfo = str6 + "<td align='left' class='article_content'><a href='products_details.aspx?pid=" + row["proid"].ToString() + "' class='blue'>" + row["proname"].ToString() + "</a><br />" + row["contents"].ToString() + "</td>";
                        this.carinfo = this.carinfo + "<td align='center'>" + string.Format("{0:f}", row["price"]) + " 元 </td>";
                        string str7 = this.carinfo;
                        this.carinfo = str7 + "<form name='frm" + row["carid"].ToString() + "' action='account.aspx?carid=" + row["carid"].ToString() + "&action=edit' method='post'><td align='center'><input name='car_" + row["carid"].ToString() + "' class='box' size='3' maxlength='5' value='" + row["pronum"].ToString() + "' type='text' />";
                        this.carinfo = this.carinfo + "&nbsp;&nbsp;<input type=image src='images/delete2.gif' /></td></form>";
                        this.carinfo = this.carinfo + "<td align='center'>" + string.Format("{0:f}", row["sumprice"]) + " 元 </td>";
                        this.carinfo = this.carinfo + "<td align='center'><a href=javascript:if(confirm('确实要删除吗?')){window.location.href='account.aspx?carid=" + row["carid"].ToString() + "&action=del';}><img src='images/delete.gif' align='absmiddle' border='0' /></a></td>";
                        this.carinfo = this.carinfo + "</tr>";
                    }
                    this.tempOrderCode = theOrderAccount.GenerateOrderCodeForBefor();

                    if (isHaveCard == "1" && base.UserMemberId != null)
                    {
                        base.Response.Write("<script language='javascript'>alert('您不能够兑换礼品卡，请先删除购物车中的礼品卡!');</script>");
                    }
                }
                else
                {
                    this.carinfo = "<tr><td align='center' colspan='6' height=45>您的购物车中无商品</td></tr>";
                }
            }

        }

        string GetTempId()
        {
            string sTempId = string.Empty;
            if (UserCardCodeList != null)
            {
                sTempId = UserCardCodeList[0].UserCardCode;
            }
            else if (UserMemberId != null)
            {
                sTempId = UserMemberId;
            }
            else
            {
                sTempId = re.ReadCookie("tempno");
            }
            if (string.IsNullOrEmpty(sTempId))
            {
                sTempId = "0";
            }
            return sTempId;
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.Bind();
                this.GetAccountMoney();
                if (UserCardCodeList != null)
                {
                    this.divTopMessage.Visible = true;
                    decimal dTotalCardPrice = 0;
                    for (int i = 0; i < UserCardCodeList.Count; i++)
                    {
                        dTotalCardPrice += decimal.Parse(UserCardCodeList[i].UserCardPrice);
                    }
                    UserCardTotalPrice = dTotalCardPrice.ToString();
                    this.lblMoneyMessage.Text = "您的礼品卡余额为:" + dTotalCardPrice.ToString() + ",请选取您要购买的商品！";
                }
                else
                {
                    this.divTopMessage.Visible = false;
                }
            }
        }

        private void GetAccountMoney()
        {
            
            if (base.UserMemberId == null)
            {
                accountmoney = "0";
            }
            else
            {
                //decimal dMomey = 0;
                //ds = theMemberAccount.GetListByMemberId(int.Parse(Session["memberid"].ToString()));
                //if (ds != null && ds.Tables.Count > 0)
                //{
                //    foreach (DataRow dr in ds.Tables[0].Rows)
                //    {
                //        dMomey =  dMomey + decimal.Parse( dr["cardprice"].ToString());
                //    }
                //}
                //accountmoney = int.Parse(dMomey.ToString()).ToString();

                //DB_Member oMember;
                //oMember = theMember.GetModel(int.Parse(base.UserMemberId));
                //if (oMember != null)
                //{
                //    accountmoney = oMember.money.ToString();
                //}
                accountmoney = theMemberAccount.GetRemainSumMoney(int.Parse(base.UserMemberId)).ToString();
            }
        }

        protected void lbtnExit_Click(object sender, EventArgs e)
        {
            base.UserCardCodeList = null;
            Response.Redirect("index.aspx");
        }

     

    }
}